﻿using System;
using System.Data;
using Moq;
using NUnit.Framework;
using SUT.Validation;

namespace SUT.Fixture2.Validation
{
    [TestFixture]
    public class DatabaseFixtureWithMock
    {
        [Test]
        public void IsAlive_NoConnBection_False()
        {
            //Arrange
            var mockConnection = new Mock<IDbConnection>();
            var mockCommand = new Mock<IDbCommand>(MockBehavior.Strict);
            mockConnection.Setup(c => c.CreateCommand()).Returns(mockCommand.Object);
            mockCommand.Setup(c => c.ExecuteScalar()).Returns(1);
            mockCommand.Setup(c => c.Dispose());
            mockCommand.SetupSet(c => c.CommandType = CommandType.Text);
            mockCommand.SetupSet(c => c.CommandText = It.IsAny<String>());
            //Act
            var database = new Database(mockConnection.Object);
            bool isAlive = database.IsAlive();
            //Assert
            Assert.IsTrue(isAlive);
            mockConnection.Verify(c => c.Dispose());
            mockCommand.Verify(c => c.Dispose());
            mockCommand.VerifySet(c => c.CommandText = "SELECT COUNT(1)");
        }
    }
}